package com.gc.web.controller.business;

import com.gc.business.domain.IntegralGoods;
import com.gc.business.domain.IntegralOrder;
import com.gc.business.service.IIntegralGoodsService;
import com.gc.business.service.IIntegralOrderService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 积分订单 控制层
 * 
 * @author liujinteng
 * @date 2021-01-04
 */
@Slf4j
@Controller
@RequestMapping("/business/integralOrder")
public class IntegralOrderController extends BaseController {
    private String prefix = "business/integralOrder";
	
	private final IIntegralOrderService integralOrderService;

	private final IIntegralGoodsService integralGoodsService;

	@Autowired
	public IntegralOrderController(IIntegralOrderService integralOrderService,
								   IIntegralGoodsService integralGoodsService) {
		this.integralOrderService = integralOrderService;
		this.integralGoodsService = integralGoodsService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:integralOrder:view")
	@GetMapping
	public String integralOrder(){
	    return prefix + "/integralOrder";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{orderId}")
	public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap){
		IntegralOrder integralOrder = integralOrderService.getById(orderId);
		mmap.put("integralOrder", integralOrder);
		return prefix + "/edit";
	}
	
	/**
	 * 查询积分订单列表
	 */
	@RequiresPermissions("business:integralOrder:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(IntegralOrder integralOrder){
		integralOrder.setUserId(ShiroUtils.getUserId());
		startPage();
        List<IntegralOrder> list = integralOrderService.list(integralOrder);
		for (IntegralOrder order : list) {
			IntegralGoods goods = integralGoodsService.getById(order.getGoodsId());
			order.setIntegralGoods(goods);
		}
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存积分订单
	 */
	@RequiresPermissions("business:integralOrder:add")
	@Log(title = "积分订单", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(IntegralOrder integralOrder){
		integralOrderService.save(integralOrder);
		return Result.success();
	}

	/**
	 * 修改保存积分订单
	 */
	@RequiresPermissions("business:integralOrder:edit")
	@Log(title = "积分订单", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(IntegralOrder integralOrder){
		integralOrderService.updateById(integralOrder);
		return Result.success();
	}
	
	/**
	 * 删除积分订单
	 */
	@RequiresPermissions("business:integralOrder:remove")
	@Log(title = "积分订单", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		integralOrderService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出积分订单列表
	 */
	@RequiresPermissions("business:integralOrder:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(IntegralOrder integralOrder){
		List<IntegralOrder> list = integralOrderService.list(integralOrder);
		ExcelUtil<IntegralOrder> util = new ExcelUtil<>(IntegralOrder.class);
		util.exportExcel(list, "integralOrder");
		return Result.success();
	}

    /**
     * 详情页
     */
	@RequiresPermissions("business:integralOrder:detail")
    @GetMapping("/detail/{orderId}")
    public String detail(@PathVariable("orderId") Long orderId, ModelMap mmap){
        IntegralOrder integralOrder = integralOrderService.getById(orderId);
        if (integralOrder != null){
        	//积分商品信息
			IntegralGoods goods = integralGoodsService.getById(integralOrder.getGoodsId());
			integralOrder.setIntegralGoods(goods);
			//兑换状态
            if (integralOrder.getExchangeStatus().equals("0")){
                integralOrder.setExchangeStatus("未兑换");
            }else if (integralOrder.getExchangeStatus().equals("1")){
                integralOrder.setExchangeStatus("已兑换");
            }
        }
        mmap.put("integralOrder", integralOrder);
        return prefix + "/detail";
    }

	/**
	 * 核销兑换跳转页
	 */
	@RequiresPermissions("business:integralOrder:cancelGoods")
	@GetMapping("/cancelGoodsPage/{orderId}")
	public String cancelGoodsPage(@PathVariable("orderId") Long orderId, ModelMap mmap){
		IntegralOrder integralOrder = integralOrderService.getById(orderId);
		mmap.put("integralOrder", integralOrder);
		return prefix + "/cancelGoods";
	}

	/**
	 * 核销兑换
	 */
	@PostMapping("/cancelGoods")
	@ResponseBody
	public Result cancelGoods(String orderId,String exchangeStatus ){
		IntegralOrder integralOrder = integralOrderService.getById(orderId);
		if (integralOrder == null){
			return Result.failed("核销失败");
		}
		integralOrder.setExchangeStatus(exchangeStatus);
		integralOrderService.updateById(integralOrder);
		return Result.success();
	}
}
